package com.qingxin.sys.service;

import java.util.Date;

import org.springframework.stereotype.Service;

import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.entity.SysLog;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.utils.StringUtils;

/**
 * 系统日志
 * @author Administrator
 *
 */
@Service
public class SysLogService extends BaseService<SysLog, String> {

	/**
	 * 登录日志列表
	 * @param logtype 日志类型：1登录日志、2注销日志
	 * @param loginName
	 * @param begin
	 * @param end
	 * @param pageParam
	 * @return
	 */
	public Pager listLoginLogs(Integer logtype, String loginName, Date begin, Date end, PageParam pageParam){
		Querier querier = Querier.create("from SysLog g ");
		if(logtype != null){
			querier.append(" where g.logtype=:logtype");
			querier.setParam("logtype", logtype);
		}else {
			querier.append(" where g.logtype in (1,2) ");
		}
		if(StringUtils.isNotEmpty(loginName)){
			querier.append(" and g.loginName = :loginName ");
			querier.setParam("loginName", loginName);
		}
		if(begin != null){
			querier.append(" and g.logtime >= :beginTime ");
			querier.setParam("beginTime", begin);
		}
		if(end != null){
			querier.append(" and g.logtime <= :endTime ");
			querier.setParam("endTime", end);
		}
		querier.append(" order by g.logtime desc");
		
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	/**
	 * 业务日志列表
	 * @param tableName
	 * @param loginName
	 * @param begin
	 * @param end
	 * @param pageParam
	 * @return
	 */
	public Pager listBusinessLogs(String tableName, String loginName, Date begin, Date end, PageParam pageParam){
		Querier querier = Querier.create("from SysLog g where g.logtype=3 ");
		if(StringUtils.isNotEmpty(tableName)){
			querier.append(" and g.tableName = :tableName ");
			querier.setParam("tableName", tableName);
		}
		if(StringUtils.isNotEmpty(loginName)){
			querier.append(" and g.loginName = :loginName ");
			querier.setParam("loginName", loginName);
		}
		if(begin != null){
			querier.append(" and g.logtime >= :beginTime ");
			querier.setParam("beginTime", begin);
		}
		if(end != null){
			querier.append(" and g.logtime <= :endTime ");
			querier.setParam("endTime", end);
		}
		querier.append(" order by g.logtime desc");
		
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	/**
	 * 保存日志
	 * @param sysLog
	 * @return
	 */
	public SysError add(SysLog sysLog){
		if(sysLog != null){
			this.save(sysLog);
		}
		return null;
	}
	
	
	/**
	 * 保存日志
	 * @param logtype 日志类型：1登录日志、2注销日志、3业务日志
	 * @param tableName 操作涉及到的表名
	 * @param loginfo
	 * @param ip
	 * @param loginName
	 * @param username
	 */
	public void addLog(int logtype, String tableName, String loginfo, String ip, String loginName, String username){
		if(tableName == null){
			tableName = "";
		}
		if(loginfo == null){
			loginfo = "";
		}
		if(ip == null){
			ip = "";
		}
		//自动截断超长日志内容
		loginfo = StringUtils.getLengthVarchar(loginfo, 4000);
		SysLog sysLog = new SysLog(loginName, username, new Date(), logtype, loginfo, tableName, ip, null);
		this.save(sysLog);
	}
	
	
	
}
